package com.ev.custom.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ev.common.service.FtpService;
import com.ev.framework.config.ConstantForDevice;
import com.ev.custom.dao.DocumentDao;
import com.ev.custom.domain.ContentAssocDO;
import com.ev.custom.domain.DictionaryDO;
import com.ev.custom.domain.DocumentDO;
import com.ev.custom.service.ContentAssocService;
import com.ev.custom.service.DictionaryService;
import com.ev.custom.service.DocumentService;
import com.ev.framework.exception.AttachmentException;
import com.ev.system.domain.UserDO;
import com.ev.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


@Service
public class DocumentServiceImpl implements DocumentService {
	@Autowired
	private DocumentDao documentDao;

	@Autowired
	private DictionaryService dictionaryService;

	@Autowired
	private ContentAssocService contentAssocService;

	@Autowired
	private UserService userService;

	@Autowired
	private FtpService ftpService;
	
	@Override
	public DocumentDO get(Long id){
		return documentDao.get(id);
	}
	
	@Override
	public List<DocumentDO> list(Map<String, Object> map){
		return documentDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return documentDao.count(map);
	}
	
	@Override
	public int save(DocumentDO document){
		return documentDao.save(document);
	}
	
	@Override
	public int update(DocumentDO document){
		return documentDao.update(document);
	}
	
	@Override
	public int remove(Long id){
		return documentDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return documentDao.batchRemove(ids);
	}

	@Override
	public List<Map<String, Object>> listForMap(Map<String, Object> map) {
		return documentDao.listForMap(map);
	}

	@Override
	public int countForMap(Map<String, Object> map) {
		return documentDao.countForMap(map);
	}

	@Override
	public Map<String, Object> detail(Long id) {

		Map<String,Object> results = new HashMap<>();

		List<DictionaryDO> documentTypeList = dictionaryService.listByType("document_type");
		results.put("documentTypeList",documentTypeList);
		List<DictionaryDO> sourceList = dictionaryService.listByType("document_source");
		results.put("sourceList",sourceList);
		List<UserDO> userList = userService.list(null);
		results.put("userList",userList);
		DocumentDO documentDO = get(id);
		results.put("document", documentDO);
		//获取附件
		Map<String,Object> contentMap = new HashMap<String,Object>(){{put("assocId",documentDO.getId());put("assocType", ConstantForDevice.DOCUMENT_APPEARANCE_ATTACHMENT);}};
		List<ContentAssocDO> contentAssocDOS = contentAssocService.list(contentMap);
		results.put("initFileList", contentAssocDOS);
		return results;
	}

	@Override
	public void add(DocumentDO document, String[] taglocationappearanceImage) {
		//保存文档信息
		save(document);
		//保存图片信息
		contentAssocService.saveList(Long.parseLong(document.getId().toString()),taglocationappearanceImage, ConstantForDevice.DOCUMENT_APPEARANCE_ATTACHMENT);
	}

	@Override
	public void edit(DocumentDO document, String[] taglocationappearanceImage, String[] deletetagAppearanceImage) {
		update(document);
		contentAssocService.saveList(Long.parseLong(document.getId().toString()),taglocationappearanceImage, ConstantForDevice.DOCUMENT_APPEARANCE_ATTACHMENT);
		contentAssocService.deleteList(deletetagAppearanceImage);
	}

	@Override
	public void downLoadWithZip(Long id, String assocType, String fileName, HttpServletResponse response) throws IOException {
		//获取附件
		Map<String, Object> contentMap = new HashMap<String, Object>() {{
			put("assocId", id);
			put("assocType", assocType);
			put("sort","id");
			put("order","ASC");
		}};
		List<ContentAssocDO> contentAssocDOS = contentAssocService.list(contentMap);
		if(contentAssocDOS.size()==0){
			throw new AttachmentException();
		}
		String uuName = UUID.randomUUID().toString();
		File zipFile = new File(uuName);
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
		byte[] buffer = new byte[1024];
		for(ContentAssocDO contentAssocDO: contentAssocDOS){
			// 多文件上传防止文件名一样加UUID 区分
			String file = contentAssocDO.getFileName();
			String finalFile = file;
			boolean b = contentAssocDOS.stream().anyMatch(e -> Objects.equals(e.getFileName(), finalFile));
			if (b){
				file = file + UUID.randomUUID().toString();
				contentAssocDO.setFileName(file);
			}
			ZipEntry entry = new ZipEntry(file);
			zipOut.putNextEntry(entry);
			InputStream is = ftpService.downFile(contentAssocDO.getFilePath(),contentAssocDO.getFileName());
			int i = -1;
			while ((i = is.read(buffer)) != -1) {
				zipOut.write(buffer, 0, i);
			}
			zipOut.closeEntry();
			is.close();
		}
		zipOut.close();

		int len;
		FileInputStream zipInput =new FileInputStream(zipFile);
		OutputStream out = response.getOutputStream();
		fileName = StringUtils.isEmpty(fileName)?URLEncoder.encode(uuName, "UTF-8"):fileName;
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition", "attachment; filename="
				+ new String(fileName.getBytes("UTF8"), "ISO8859-1") + ".zip");
		while ((len=zipInput.read(buffer))!= -1){
			out.write(buffer,0,len);
		}
		zipInput.close();
		out.flush();
		out.close();
		//删除压缩包
		zipFile.delete();
	}

}
